import javax.sound.midi.Track;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: leetcode.简单题.有效的括号
 */

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c == '(' || c == '[' || c == '{') {
                stack.push(c);
            }
            if(c == ')' || c == ']' || c == '}') {
                if(stack.empty()) {
                    return false;
                }
                if((stack.peek() == '(' && c == ')') ||
                        (stack.peek() == '[' && c == ']') ||
                        (stack.peek() == '{' && c == '}')) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }
}

public class Test {
}
